package com.dem.android.util;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.text.TextUtils;

import com.dem.android.db.DemsColumns;
import com.dem.android.model.Dems;
import com.dem.android.model.DemsType;
import com.dem.android.service.UpdateService;

public class DemsProcessor {
	public static final String TABLE = "table";
	public static final String TD = "td";
	public static final String IMG = "img";
	public static final String A = "a";
	public static final String HREF = "href";
	public static final String SRC = "src";
	public static final String POSTER = "poster";
	public static int total = 0;

	public void processDemsRu(final ContentResolver resolver, final String url_ru, int page, String type) throws IOException, HttpException, Exception {
		ArrayList<ContentProviderOperation> insertOperations = new ArrayList<ContentProviderOperation>();

		URL url;
		Document doc = null;

		try {
			url = new URL(url_ru);
			doc = (Document) Jsoup.parse(url, 100 * 1000);
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			Elements tables = doc.getElementsByTag(TABLE);
			Elements subElements = tables.get(0).getElementsByTag(TD);

			for (Element item : subElements) {
				Elements imgEl = item.getElementsByTag(IMG);
				Elements aEl = item.getElementsByTag(A);

				String subUrl = "";
				try {
					String href = aEl.get(0).attr(HREF);
					String url2 = imgEl.attr(SRC);

					if (url2 != null && !TextUtils.isEmpty(url2) && href != null && !TextUtils.isEmpty(href)) {
						// Log.w("Thumbnail",
						// UpdateService.BASE_URL_RU.concat(href));

						URL url1;
						Document doc1 = null;
						try {
							url1 = new URL(UpdateService.BASE_URL_RU.concat(href));
							doc1 = (Document) Jsoup.parse(url1, 100 * 1000);
						} catch (IOException e) {
							e.printStackTrace();
						}

						Element el;
						try {
							el = doc1.getElementById(POSTER);
							subUrl = el.attr(SRC);
						} catch (Exception e) {
							e.printStackTrace();
						}

						// Log.e("page", "" + page);
						// Log.e("subUrl", subUrl);

					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				insertOperations.add(ContentProviderOperation.newInsert(Dems.CONTENT_URI)
						.withValue(DemsColumns.THUMBNAIL.getName(), UpdateService.BASE_URL_RU + subUrl).withValue(DemsColumns.PAGE.getName(), page)
						.withValue(DemsColumns.TYPE.getName(), type).build());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (insertOperations.size() > 0) {
			resolver.delete(Dems.CONTENT_URI, DemsColumns.PAGE.getName() + " = " + page + " and " + DemsColumns.TYPE.getName() + " = '" + DemsType.RU.getName()
					+ "'", null);
			resolver.applyBatch(Dems.CONTENT_AUTHORITY, insertOperations);

		}
	}

	public void processDemsUs(final ContentResolver resolver, final String url_us, int page, String type) throws IOException, HttpException, Exception {
		ArrayList<ContentProviderOperation> insertOperations = new ArrayList<ContentProviderOperation>();

		URL url;
		Document doc = null;

		try {
			url = new URL(url_us);
			doc = (Document) Jsoup.parse(url, 100 * 1000);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String url2 = "";
		try {
			Elements tables = doc.getElementsByClass("picture");

			for (Element item : tables) {
				Elements imgEl = item.getElementsByTag("img");
				try {

					url2 = imgEl.get(0).attr("src");
				} catch (Exception e) {
					e.printStackTrace();
				}
				insertOperations.add(ContentProviderOperation.newInsert(Dems.CONTENT_URI).withValue(DemsColumns.THUMBNAIL.getName(), url2)
						.withValue(DemsColumns.PAGE.getName(), page).withValue(DemsColumns.TYPE.getName(), type).build());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (insertOperations.size() > 0) {
			resolver.delete(Dems.CONTENT_URI, DemsColumns.PAGE.getName() + " = " + page + " and " + DemsColumns.TYPE.getName() + " = '" + DemsType.US.getName()
					+ "'", null);
			resolver.applyBatch(Dems.CONTENT_AUTHORITY, insertOperations);
		}
	}
}
